/********************************************************************************
* Copyright (c) 2009, Matteo Pelati (matteo.pelati@gmail.com)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in the
*       documentation and/or other materials provided with the distribution.
*     * Neither the name of the <organization> nor the
*       names of its contributors may be used to endorse or promote products
*       derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY Matteo Pelati ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
**************************************************************************************/


#pragma once


#include <fstream>
#include <string>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include "dbxml/DbXml.hpp"
#include "dbXmlLib.h"
#include "DbEnvironment.h"
#include "DbXmlTransaction.h"
#include "DbXmlQueryContext.h"
#include "DbXmlDocument.h"
#include "DbXmlContainer.h"
#include "DbXmlUpdateContext.h"
#include "DbXmlModify.h"

using namespace DbXml;
using namespace System;
using namespace System::Collections::Generic;

ref class DbXmlResults; 
ref class DbXmlValueCollection;

namespace BerkeleyDB { namespace Xml
{

	[Flags] public enum class DbXmlManagerInitFlags : u_int32_t
	{
		AllowExternalAccess = DBXML_ALLOW_EXTERNAL_ACCESS,
		AllowAutoOpen = DBXML_ALLOW_AUTO_OPEN
	};

	public ref class DbXmlManager : public DbGenericObject
	{
	private:
		XmlManager* n_dbEnv;
		Object^ parent;
	internal:
		XmlManager* GetInnerObject()
		{
			return n_dbEnv;
		}
	public:
		DbXmlManager(BerkeleyDB::DbEnvironment ^dbEnv, DbXmlManagerInitFlags flags) : DbGenericObject(dbEnv, nullptr)
		{
			this->n_dbEnv = new XmlManager(dbEnv->GetInnerObject(), (u_int32_t) flags);
			this->manager = (DbXmlManager^) this;
		};
	public:
		~DbXmlManager()
		{
			GC::SuppressFinalize(this);
			this->!DbXmlManager();
		};
		!DbXmlManager()
		{
			delete this->n_dbEnv;
		};
	public:

		// Properties
		PROPERTY_RET_CSTRING_GET(HomeDirectory,, Home);
		PROPERTY_RET_INT_BOTH(ImplicitTimezone,, ImplicitTimezone);
		PROPERTY_RET_UINT32_GET(Flags,, Flags);
		PROPERTY_RET_UINT32_BOTH(DefaultPageSize,, DefaultPageSize);
		PROPERTY_RET_UINT32_BOTH(DefaultSequenceIncrement,, DefaultSequenceIncrement);
		PROPERTY_RET_ENUM_BOTH(DefaultContainerFlags,, DefaultContainerFlags, DbXmlContainerInitFlags, u_int32_t);
		PROPERTY_RET_ENUM_BOTH(DefaultContainerType,, DefaultContainerType, DbXmlContainerType, XmlContainer::ContainerType);

		// Forward methods declaration
		
		DbXmlDocument^ CreateDocument();

		DbXmlContainer^ CreateContainer(String^ name);
		DbXmlContainer^ CreateContainer(String^ name, DbXmlTransaction^ txn);
		DbXmlContainer^ CreateContainer(String^ name, DbXmlContainerInitFlags flags, DbXmlContainerType type);
		DbXmlContainer^ CreateContainer(String^ name, DbXmlTransaction^ txn, DbXmlContainerInitFlags flags, DbXmlContainerType type);
		DbXmlContainer^ OpenContainer(String^ name);
		DbXmlContainer^ OpenContainer(String^ name, DbXmlTransaction^ txn);
		DbXmlContainer^ OpenContainer(String^ name, DbXmlContainerInitFlags flags);
		DbXmlContainer^ OpenContainer(String^ name, DbXmlTransaction^ txn, DbXmlContainerInitFlags flags);
		DbXmlContainer^ OpenContainer(String^ name, DbXmlContainerInitFlags flags, DbXmlContainerType type);
		DbXmlContainer^ OpenContainer(String^ name, DbXmlTransaction^ txn, DbXmlContainerInitFlags flags, DbXmlContainerType type);
		int ExistsContainer(String^ name);
		void ReindexContainer(String^ name, DbXmlUpdateContext^ ctx, DbXmlContainerInitFlags flags);
		void ReindexContainer(DbXmlTransaction^ txn, String^ name, DbXmlUpdateContext^ ctx, DbXmlContainerInitFlags flags);
		void RemoveContainer(DbXmlTransaction^ txn, String^ name);
		void RemoveContainer(String^ name);
		void RenameContainer(String^ oldname, String^ newname);
		void RenameContainer(DbXmlTransaction^ txn, String^ oldname, String^ newname);
		void CompactContainer(String^ name, DbXmlUpdateContext^ uctx);
		void CompactContainer(DbXmlTransaction^ txn, String^ name, DbXmlUpdateContext^ uctx);


		DbXmlUpdateContext^ CreateUpdateContext();

		DbXmlTransaction^ CreateTransaction(DbXmlTxnCreateFlags flags);

		DbXmlModify^ CreateModify();

		DbXmlQueryContext^ CreateQueryContext(DbXmlQueryReturnType rtype, DbXmlQueryEvalType evtype);

		DbXmlResults^ CreateResults();

		IEnumerable<DbXmlValue^>^ Query(String^ xquery, DbXmlQueryContext^ ctx, DbXmlQueryContextFlags flags);
		IEnumerable<DbXmlValue^>^ Query(DbXmlTransaction^ txn, String^ xquery, DbXmlQueryContext^ ctx, DbXmlQueryContextFlags flags);

		DbXmlQueryExpression^ Prepare(String^ xquery, DbXmlQueryContext^ ctx);
		DbXmlQueryExpression^ Prepare(DbXmlTransaction^ txn, String^ xquery, DbXmlQueryContext^ ctx);



	};

}}